#include "../Tools/GLHeaders.hpp"
#include "Tile.hpp"

//#define __USE_BSD
#include <cmath>
#define BLEND 0.1f

void Tile::Draw(void){
  glBindBuffer(GL_ARRAY_BUFFER, tile_buffers[0]);
  glNormalPointer(GL_FLOAT, 10 * sizeof(GLfloat), 0);
  glColorPointer(4, GL_FLOAT, 10 * sizeof(GLfloat), ((GLfloat*)NULL+(3)));
  glVertexPointer(3, GL_FLOAT, 10 * sizeof(GLfloat), ((GLfloat*)NULL+(7)));

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tile_buffers[1]);

  glEnableClientState(GL_NORMAL_ARRAY);
  glEnableClientState(GL_COLOR_ARRAY);
  glEnableClientState(GL_VERTEX_ARRAY);

  glDisable(GL_TEXTURE_2D);
  glCullFace(GL_FRONT);
  glDrawElements(GL_TRIANGLES, 84, GL_UNSIGNED_INT, 0);
  glCullFace(GL_BACK);
  glDrawElements(GL_TRIANGLES, 84, GL_UNSIGNED_INT, 0);
  glEnable(GL_TEXTURE_2D);

  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_COLOR_ARRAY);
  glDisableClientState(GL_VERTEX_ARRAY);
}

Tile::Tile(void){
  GLfloat r1, g1, b1, a1;
  GLfloat r2, g2, b2, a2;
  r1 = 1.0f;
  g1 = 0.0f;
  b1 = 0.0f;
  a1 = 1.0f;

  r2 = 0.0f;
  g2 = 0.0f;
  b2 = 0.0f;
  a2 = 0.0f;

  GLfloat tile_array_tmp[480] = {//NORMAL, COLOR, VERTEX
    // Top Face
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1,-0.25f, 0.0f, 0.5f, // 0
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1, 0.25f, 0.0f, 0.5f, // 1
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1, 0.5f, 0.0f, 0.25f, // 2
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1, 0.5f, 0.0f,-0.25f, // 3
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1, 0.25f, 0.0f,-0.5f, // 4
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1,-0.25f, 0.0f,-0.5f, // 5
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1,-0.5f, 0.0f,-0.25f, // 6
    0.0f, 1.0f, 0.0f, r1, g1, b1, a1,-0.5f, 0.0f, 0.25f, // 7
    // Front Face
    0.0f, 0.0f, 1.0f, r2, g2, b2, a2,-0.25f,-0.25f, 0.5f, // 8
    0.0f, 0.0f, 1.0f, r2, g2, b2, a2, 0.25f,-0.25f, 0.5f, // 9
    0.0f, 0.0f, 1.0f, r1, g1, b1, a1, 0.25f, 0.0f, 0.5f, // 10
    0.0f, 0.0f, 1.0f, r1, g1, b1, a1,-0.25f, 0.0f, 0.5f, // 11
    // Front-Right Face
    M_SQRT1_2, 0.0f, M_SQRT1_2, r2, g2, b2, a2, 0.25f,-0.25f, 0.5f, // 12
    M_SQRT1_2, 0.0f, M_SQRT1_2, r2, g2, b2, a2, 0.5f,-0.25f, 0.25f, // 13
    M_SQRT1_2, 0.0f, M_SQRT1_2, r1, g1, b1, a1, 0.5f, 0.0f, 0.25f, // 14
    M_SQRT1_2, 0.0f, M_SQRT1_2, r1, g1, b1, a1, 0.25f, 0.0f, 0.5f, // 15
    // Right Face
    1.0f, 0.0f, 0.0f, r2, g2, b2, a2, 0.5f,-0.25f, 0.25f, // 16
    1.0f, 0.0f, 0.0f, r2, g2, b2, a2, 0.5f,-0.25f,-0.25f, // 17
    1.0f, 0.0f, 0.0f, r1, g1, b1, a1, 0.5f, 0.0f,-0.25f, // 18
    1.0f, 0.0f, 0.0f, r1, g1, b1, a1, 0.5f, 0.0f, 0.25f, // 19
    // Back-Right Face
    M_SQRT1_2, 0.0f,-M_SQRT1_2, r2, g2, b2, a2, 0.5f,-0.25f,-0.25f, // 20
    M_SQRT1_2, 0.0f,-M_SQRT1_2, r2, g2, b2, a2, 0.25f,-0.25f,-0.5f, // 21
    M_SQRT1_2, 0.0f,-M_SQRT1_2, r1, g1, b1, a1, 0.25f, 0.0f,-0.5f, // 22
    M_SQRT1_2, 0.0f,-M_SQRT1_2, r1, g1, b1, a1, 0.5f, 0.0f,-0.25f, // 23
    // Back Face
    0.0f, 0.0f,-1.0f, r2, g2, b2, a2, 0.25f,-0.25f,-0.5f, // 24
    0.0f, 0.0f,-1.0f, r2, g2, b2, a2,-0.25f,-0.25f,-0.5f, // 25
    0.0f, 0.0f,-1.0f, r1, g1, b1, a1,-0.25f, 0.0f,-0.5f, // 26
    0.0f, 0.0f,-1.0f, r1, g1, b1, a1, 0.25f, 0.0f,-0.5f, // 27
    // Back-Left Face
    -M_SQRT1_2, 0.0f,-M_SQRT1_2, r2, g2, b2, a2,-0.25f,-0.25f,-0.5f, // 28
    -M_SQRT1_2, 0.0f,-M_SQRT1_2, r2, g2, b2, a2,-0.5f,-0.25f,-0.25f, // 29
    -M_SQRT1_2, 0.0f,-M_SQRT1_2, r1, g1, b1, a1,-0.5f, 0.0f,-0.25f, // 30
    -M_SQRT1_2, 0.0f,-M_SQRT1_2, r1, g1, b1, a1,-0.25f, 0.0f,-0.5f, // 31
    // Left Face
   -1.0f, 0.0f, 0.0f, r2, g2, b2, a2,-0.5f,-0.25f,-0.25f, // 32
   -1.0f, 0.0f, 0.0f, r2, g2, b2, a2,-0.5f,-0.25f, 0.25f, // 33
   -1.0f, 0.0f, 0.0f, r1, g1, b1, a1,-0.5f, 0.0f, 0.25f, // 34
   -1.0f, 0.0f, 0.0f, r1, g1, b1, a1,-0.5f, 0.0f,-0.25f, // 35
    // Front-Left Face
    -M_SQRT1_2, 0.0f, M_SQRT1_2, r2, g2, b2, a2,-0.5f,-0.25f, 0.25f, // 36
    -M_SQRT1_2, 0.0f, M_SQRT1_2, r2, g2, b2, a2,-0.25f,-0.25f, 0.5f, // 37
    -M_SQRT1_2, 0.0f, M_SQRT1_2, r1, g1, b1, a1,-0.25f, 0.0f, 0.5f, // 38
    -M_SQRT1_2, 0.0f, M_SQRT1_2, r1, g1, b1, a1,-0.5f, 0.0f, 0.25f, // 39
    // Under Face
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2,-0.25f,-0.25f, 0.5f, // 40
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2, 0.25f,-0.25f, 0.5f, // 41
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2, 0.5f,-0.25f, 0.25f, // 42
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2, 0.5f,-0.25f,-0.25f, // 43
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2, 0.25f,-0.25f,-0.5f, // 44
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2,-0.25f,-0.25f,-0.5f, // 45
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2,-0.5f,-0.25f,-0.25f, // 46
    0.0f,-1.0f, 0.0f, r2, g2, b2, a2,-0.5f,-0.25f, 0.25f, // 47
    };

  GLuint indice_array_tmp[84] = {
     1,  2,  0,  0,  2,  7,
     7,  2,  3,  7,  3,  6,
     6,  3,  4,  6,  4,  5,  // Top
     9, 10,  8,  8, 10, 11, // Front
    13, 14, 12, 12, 14, 15, // Front Right
    17, 18, 16, 16, 18, 19, // Right
    21, 22, 20, 20, 22, 23, // Back Right
    25, 26, 24, 24, 26, 27, // Back
    29, 30, 28, 28, 30, 31, // Back Left
    33, 34, 32, 32, 34, 35, // Left
    37, 38, 36, 36, 38, 39, // Front Left
    41, 40, 42, 42, 40, 47,
    42, 47, 43, 43, 47, 46,
    43, 46, 44, 44, 46, 45  // Under
  };

  tile_array = (GLfloat *) new GLfloat[480];
  indice_array = (GLuint *) new GLuint[84];

  for(int i=0; i < 480; i++)
    tile_array[i] = tile_array_tmp[i];
   for(int i=0; i < 84; i++)
    indice_array[i] = indice_array_tmp[i];

  tile_buffers = new GLuint[2];
  glGenBuffers( 2, tile_buffers);

  glBindBuffer(GL_ARRAY_BUFFER, tile_buffers[0]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(tile_array)*480, tile_array, GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tile_buffers[1]);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indice_array)*84, indice_array, GL_STATIC_DRAW);
}

Tile::~Tile(void){
  glDeleteBuffers(2, tile_buffers);
  delete[] tile_array;
  delete[] indice_array;
  delete[] tile_buffers;
}
